package com.scfinance.SPD.encrypt.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName: DateUtil
 * @Description: 新版时间工具类
 * @author xuzhenyao
 * @date 2015-2-3 上午10:44:38
 * 
 */
public class DateUtil {
	/**
	 * 默认时间格式
	 */
	public static final String format = "yyyy-MM-dd HH:mm:ss";
	public static final String format1 = "yyyyMMdd";
	public static final String format2 = "yyyyMMddHHmmss";
	public static final String format3 = "yyyy-MM-dd";

	/**
	 * @Title: getServerDate
	 * @Description: 获得系统时间
	 * @param @return
	 * @return Date
	 * @throws
	 */
	public static Date getServerDate() {
		return new Date();
	}

	/**
	 * 删除取服务器时间
	 * 
	 * @return
	 */
	public static Date getServerDateByDelete() {
		return new Date();
	}

	/**
	 * @Title: getServerCalendar
	 * @Description: 获得系统日历
	 * @param @return
	 * @return Calendar
	 * @throws
	 */
	public static Calendar getServerCalendar() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getServerDate());
		return calendar;
	}

	/**
	 * @Title: getFormatNow
	 * @Description: 根据默认格式获取时间字符串
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String getFormat() {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = DateUtil.getServerDate();
		return sdf.format(date);
	}
	
	public static String getFormat(String f1) {
		SimpleDateFormat sdf = new SimpleDateFormat(f1);
		Date date = DateUtil.getServerDate();
		return sdf.format(date);
	}

	/**
	 * @Title: getFormatNow
	 * @Description: 根据给定date获得时间字符串
	 * @param @param date
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String getFormat(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public static String getFormat(Date date, String f1) {
		SimpleDateFormat sdf = new SimpleDateFormat(f1);
		return sdf.format(date);
	}
	
	/**
	 * 
	 * @Title: getFormat 
	 * @Description: f1 格式 转成f2
	 * @param @param date
	 * @param @param f1
	 * @param @param f2
	 * @param @return 
	 * @return String
	 * @throws
	 */
	public static String getFormat(String date,String f1,String f2){
		SimpleDateFormat sdf = new SimpleDateFormat(f1);
		SimpleDateFormat sdf1 = new SimpleDateFormat(f2);
		try {
		 	Date d = sdf.parse(date);
		 	return sdf1.format(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * @Title: getDayByYearAndMouth
	 * @Description: 获得当前年月的天数
	 * @param @param year
	 * @param @param mouth
	 * @param @return
	 * @return int
	 * @throws
	 */
	public static int getDayByYearAndMouth(int year, int mouth) {
		mouth = mouth - 1;
		Calendar calendar = DateUtil.getServerCalendar();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, mouth + 1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.DAY_OF_YEAR, -1);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * @Title: getNowYear
	 * @Description: 获得当前年
	 * @param @return
	 * @return int
	 * @throws
	 */
	public static int getNowYear() {
		Calendar calendar = DateUtil.getServerCalendar();
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * @Title: getNowMonth
	 * @Description: 获得当前月份
	 * @param @return
	 * @return int
	 * @throws
	 */
	public static int getNowMonth() {
		Calendar calendar = DateUtil.getServerCalendar();
		return calendar.get(Calendar.MONTH) + 1;
	}

	/**
	 * @Title: getNowDayOfMonth
	 * @Description: 获得当前月中的号
	 * @param @return
	 * @return int
	 * @throws
	 */
	public static int getNowDayOfMonth() {
		Calendar calendar = DateUtil.getServerCalendar();
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * @Title: getNowDayStartAndEnd
	 * @Description: 获得今天的起始时间
	 * @param @return
	 * @return String[]
	 * @throws
	 */
	public static String[] getNowDayStartAndEnd() {
		String[] strs = new String[2];
		Calendar calendar = DateUtil.getServerCalendar();
		calendar.set(Calendar.HOUR_OF_DAY, 24);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		strs[1] = getFormat(calendar.getTime(), "yyyyMMdd");
		calendar.add(Calendar.DAY_OF_YEAR, -1);
		strs[0] = getFormat(calendar.getTime(), "yyyyMMdd");
		return strs;
	}

	public static String[] getNowMouthStartAndEnd() {
		String[] strs = new String[2];
		Calendar calendar = DateUtil.getServerCalendar();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		strs[1] = getFormat(calendar.getTime(), "yyyyMMdd");
		calendar.add(Calendar.MONTH, -1);
		strs[0] = getFormat(calendar.getTime(), "yyyyMMdd");
		return strs;
	}

	public static String[] getThreeMouthStartAndEnd() {
		String[] strs = new String[2];
		Calendar calendar = DateUtil.getServerCalendar();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		strs[1] = getFormat(calendar.getTime(), "yyyyMMdd");
		calendar.add(Calendar.MONTH, -3);
		strs[0] = getFormat(calendar.getTime(), "yyyyMMdd");
		return strs;
	}

	/**
	 * @Title: getFormatDate
	 * @Description: 格式化默认格式
	 * @param @param date
	 * @param @param f
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String getFormatDate(String date, String f) {
		SimpleDateFormat sdf = new SimpleDateFormat(f);
		Date d = null;
		try {
			d = sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if (d != null) {
			SimpleDateFormat sdf1 = new SimpleDateFormat(format);
			return sdf1.format(d);
		}
		return null;
	}

	public static Date parseDate(String date, String f) {
		SimpleDateFormat sdf = new SimpleDateFormat(f);
		Date d = null;
		try {
			d = sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return d;
	}

	/**
	 * @Title: compareTwo
	 * @Description: 比较两个字符串
	 * @param @param date1
	 * @param @param date2
	 * @param @param format
	 * @param @return
	 * @return
	 * @throws
	 */
	public static int compareTwo(String date1, String date2, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			Date d1 = sdf.parse(date1);
			Date d2 = sdf.parse(date2);
			if (d1.getTime() > d2.getTime()) {
				return 1;
			} else if (d1.getTime() < d2.getTime()) {
				return 2;
			} else {
				return 0;
			}
		} catch (ParseException e) {
			return 0;
		}
	}
}
